//
//  WorkHomeVC.m
//  XLDoctor
//
//  Created by longma on 2018/7/27.
//  Copyright © 2018年 XLH. All rights reserved.
//mj手机区号

#import "AllCountryCodeVC.h"
#import "CountryCodeCell.h"
#import "MJSearchBarView.h"
#import "UITableView+SCIndexView.h"
#import "SearchAllCountryCodeVC.h"
#define searchBarView_H 56
@interface AllCountryCodeVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) MJUITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *keyArray;
@property (nonatomic, assign) NSInteger selectIndex;
@property (nonatomic, strong) MJSearchBarView *searchBarView;

@end

@implementation AllCountryCodeVC

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)mj_setUI{
    [super mj_setUI];
    
    self.view.backgroundColor = UIColor.mj_backgroundColor;
    self.mj_titleLocalizable = @"Choose Region";
    self.dataArray = [HealthHelper getAreaCodesJsonArray];
    self.keyArray = [HealthHelper getAreaCodesJsonKeyArray:self.dataArray];
    
    [self.view addSubview:self.searchBarView];
    [self.searchBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(18);
        make.right.equalTo(self.view).offset(-18);
        make.top.equalTo(self.view).offset(STATUS_NAV_HEIGHT + 10);
        make.height.equalTo(@(searchBarView_H));
    }];
    
    [self.view addSubview:self.tableView];
    
//    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(self.view);
//     }];

    self.selectIndex = -1;
//    NSLog(@"self.keyArray=======================%@",self.keyArray);
    self.tableView.sc_indexViewDataSource = self.keyArray.copy;
    //        self.tableView.sc_startSection = 0;
    [self.tableView reloadData];

}

#pragma mark - 方法
- (void)requestData{
    
}

#pragma mark - 代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *array = self.dataArray[section][@"array"];
    return array.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CountryCodeCell *cell = [CountryCodeCell reuseForTableView:tableView forIndexPath:indexPath];
    NSArray *array = self.dataArray[indexPath.section][@"array"];
    
    NSString *name = @"";
    if(LanguageHelper.shared.mLanguageStyle == MJLanguageStyleEN){
        name = array[indexPath.row][@"英文名"];
    }
    if(LanguageHelper.shared.mLanguageStyle == MJLanguageStyleZhHansCN){
        name = array[indexPath.row][@"中文名"];
    }
    if(LanguageHelper.shared.mLanguageStyle == MJLanguageStyleZhHantCN){
        name = array[indexPath.row][@"港澳繁体"];
    }
    if(LanguageHelper.shared.mLanguageStyle == MJLanguageStyleZhHantTW){
        name = array[indexPath.row][@"台湾繁体"];
    }
    if(LanguageHelper.shared.mLanguageStyle == MJLanguageStyleID){
        name = array[indexPath.row][@"印尼语"];
    }
    if(LanguageHelper.shared.mLanguageStyle == MJLanguageStyleRU){
        name = array[indexPath.row][@"俄语"];
    }
    
    cell.titleLabel.text = name;
    cell.subTitleLabel.text = [NSString stringWithFormat:@"%@",array[indexPath.row][@"区号"]];
    cell.subTitleLabel.edgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    if(self.selectIndex == indexPath.row){
        cell.isSelect = true;
    }else{
        cell.isSelect = false;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  
    self.selectIndex = indexPath.row;
    [tableView reloadData];
    
    MJWeakSelf
    MJUIAfter_GCD(0.35, ^{
        NSArray *array = weakSelf.dataArray[indexPath.section][@"array"];
        if(weakSelf.handleCountryCodeBlock){
            weakSelf.handleCountryCodeBlock(array[indexPath.row]);
        }
        [weakSelf mj_popViewController];
    });
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    MJUITbvTitleSectionView *sectionHeaderView = [MJUITbvTitleSectionView reuseForTableView:tableView];
//    sectionHeaderView.stackView.alignment = UIStackViewAlignmentBottom;
    NSString *sectionTitle = self.dataArray[section][@"section"];
    sectionHeaderView.titleLabel.text = sectionTitle;
    sectionHeaderView.contentView.backgroundColor = UIColor.mj_backgroundColor;
    sectionHeaderView.titleLabel.font = MJUIFontBoldMake(18);
    sectionHeaderView.titleLabel.textColor = UIColor.mj_grayColor;
    sectionHeaderView.stackViewLeft.constant = 20;
    return sectionHeaderView;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
//    return 0.01;
//}

//- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
//    return self.keyArray;
//}
//
//-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index {
//    return [self.keyArray indexOfObject:title];
//}

#pragma mark - 懒加载
- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (MJUITableView *)tableView{
    if (!_tableView){
//        MJUITableView *tableView = [[MJUITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        MJUITableView *tableView = [[MJUITableView alloc] initWithFrame:CGRectMake(0, STATUS_NAV_HEIGHT + searchBarView_H + 20, self.view.bounds.size.width, self.view.bounds.size.height - STATUS_NAV_HEIGHT - searchBarView_H - 20) style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.backgroundColor = UIColor.mj_backgroundColor;
        tableView.rowHeight = UITableViewAutomaticDimension;
        tableView.estimatedRowHeight = 140;
        tableView.sectionIndexColor = UIColor.mj_appColor;

        [CountryCodeCell registerNibWithTableView:tableView];
        [MJUITbvTitleSectionView registerNibWithTableView:tableView];

        SCIndexViewConfiguration *configuration = [SCIndexViewConfiguration configuration];
//        configuration.indicatorHeight = KVIEW_HEIGHT;
        configuration.indexItemSelectedBackgroundColor = UIColor.mj_appColor;
        tableView.sc_indexViewConfiguration = configuration;
        
//        _tableView.sc_translucentForTableViewInNavigationBar = 0;

        _tableView = tableView;
    }
    return _tableView;
}
- (MJSearchBarView *)searchBarView {
    if (!_searchBarView) {
        MJWeakSelf
        //继承 MJUICustomXibView 的view 用 initWithFrame 初始化（重要）
        _searchBarView = [[MJSearchBarView alloc]initWithFrame:CGRectZero];
        _searchBarView.searchTextField.mj_pLocalizable = @"Enter region name";
        _searchBarView.autoresizingMask = UIViewAutoresizingNone;
        _searchBarView.leftButton.hidden = true;
        _searchBarView.itemButton.hidden = false;
        [_searchBarView.itemButton mj_addAction:^(UIButton *button) {
            SearchAllCountryCodeVC *vc = [[SearchAllCountryCodeVC alloc]init];
            vc.handleCountryCodeBlock = ^(NSDictionary *countryCode) {
                if(weakSelf.handleCountryCodeBlock){
                    weakSelf.handleCountryCodeBlock(countryCode);
                }
                [weakSelf mj_popViewController];
            };
            [weakSelf.navigationController pushViewController:vc animated:true];
        }];
    }
    return _searchBarView;
}
@end
